/**
 * 
 */
package cn.gov.msa.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.Dictionary;
import cn.gov.msa.model.DictionaryDetail;
import cn.gov.msa.service.DictionaryDetailService;
import cn.gov.msa.service.DictionaryService;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.github.pagehelper.PageInfo;


/**
 * @author Administrator
 *
 */
//@Controller
@RestController
@RequestMapping(value = "/dict", produces="text/plain;charset=UTF-8")
public class DictController {
	ObjectMapper mapper = new ObjectMapper(); 	
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@Autowired
	private DictionaryDetailService dictionaryDetailService;

	
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params) throws Exception {
		String result = "";
		System.err.println(params);			
		
		int pageNum = 1;
		int pageSize = 10;
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		

		Dictionary dictionary = mapper.readValue(params.toString(), Dictionary.class);
		
		List<Dictionary> dictionaryList = dictionaryService.selectDictionary(pageNum, pageSize, dictionary);		

	    //获取分页信息
        PageInfo page = new PageInfo(dictionaryList);
        

		JSONArray ja = JSONArray.fromObject(dictionaryList);
		
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		
		
		result = jsonResult.toString();
		return result;
	}
	
	@RequestMapping(value = "/delete")
	public String delete(String dictcode) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{
			JSONObject param = new JSONObject();
			param.put("dictcode", dictcode);
			japaram.add(param);			
			
			int delete = dictionaryService.deleteDictionary(dictcode);			
			
			result = delete > 0 ? 1 : 0;
			info = delete > 0 ? "删除成功！" : "删除失败！";
		}
		catch(Exception e){
			result = 2;
			info = "删除异常!";
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/batchDelete")
	public String batchDelete(String dictcodes) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{			
			String dictcodeResult = "";
			
			for(int i = 0 ; i < dictcodes.split(",").length; i++){	
				String dictcode = dictcodes.split(",")[i];
				int delete = dictionaryService.deleteDictionary(dictcode);
				
				if(delete > 0)
					dictcodeResult += dictcode + ",";
			}
			
			dictcodeResult = dictcodeResult.substring(0, dictcodeResult.length() - 1);

			JSONObject param = new JSONObject();
			param.put("dictcode", dictcodeResult);
			japaram.add(param);
			
			result = dictcodeResult.split(",").length > 0 ? 1 : 0;
			info = dictcodeResult.split(",").length > 0 ? "批量删除成功！" : "批量删除失败！";
		}
		catch(Exception e){
			result = 2;
			info = "删除异常!";
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/save")
	public String save(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
		
	    try{
			//得到明细的model数据
			JSONArray jaDetail = param.getJSONObject("table").getJSONArray("row");
			List<DictionaryDetail> dictionaryDetailList = new ArrayList<DictionaryDetail>();
			for (Object dictionaryDetailObject : jaDetail) {
				DictionaryDetail dictionaryDetail = mapper.readValue(dictionaryDetailObject.toString(), DictionaryDetail.class);
				dictionaryDetailList.add(dictionaryDetail);
			}
			
			//得到类型的model数据
			param.remove("table");
			Dictionary dictionary = mapper.readValue(param.toString(), Dictionary.class);	
		    
		    save = dictionaryService.saveData(dictionary, dictionaryDetailList);
	    }
	    catch(Exception e){
			e.printStackTrace();
	    	result = 2;
	    	info = "数据保存异常！";
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else{
	    	result = 0;
	    	info = "数据保存失败！";
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
		
	    try{
			//得到明细的model数据
			JSONArray jaDetail = param.getJSONObject("table").getJSONArray("row");
			List<DictionaryDetail> dictionaryDetailList = new ArrayList<DictionaryDetail>();
			for (Object dictionaryDetailObject : jaDetail) {
				DictionaryDetail dictionaryDetail = mapper.readValue(dictionaryDetailObject.toString(), DictionaryDetail.class);
				
				dictionaryDetailList.add(dictionaryDetail);
			}
			
			//得到类型的model数据
			param.remove("table");			
			Dictionary dictionary = mapper.readValue(param.toString(), Dictionary.class);	
		    
		    save = dictionaryService.updateData(dictionary, dictionaryDetailList);
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = "数据保存异常！";
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else{
	    	result = 0;
	    	info = "数据保存失败！";
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/getDictDetail")
	public String getDictDetail(String dictcode,String bigClass) throws Exception{
		String result = "";
		String info = "";
		JSONObject jsonResult = new JSONObject();
		
		try{
			List<DictionaryDetail> dictionaryDetailList = dictionaryDetailService.selectDictionary(dictcode, bigClass, "");		
	
			JSONArray japaram = JSONArray.fromObject(dictionaryDetailList);
			
			result = dictionaryDetailList.size() > 0 ? "1" : "0";
			info = dictionaryDetailList.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
	
			jsonResult.put("datas", japaram);	
		}
		catch(Exception e){
			result = "2";
			info = InfoObjUtil.Exption_msg;
			e.printStackTrace();
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);	
		
		System.err.println(jsonResult);
		
		return jsonResult.toString();
	}
	


	@RequestMapping(value = "/getInfo")
	public String getInfo(String dictcode) throws Exception {
		int result = 0;
		String info = "";
		
		JSONObject joparam = new JSONObject();
		
		try{

			Dictionary dictionary = dictionaryService.getInfoByDictCode(dictcode);		
//			
			result = dictionary != null ? 1 : 0;
			info = dictionary != null ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			joparam = JSONObject.fromObject(dictionary);
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", joparam);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}
	


	@RequestMapping(value = "/getDictDetal")
	public String getDictDetal(String dictcode) throws Exception {
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{

			List<DictionaryDetail> dictionaryDetailList = dictionaryDetailService.selectDictionary(dictcode, "", "");
//			
			result = dictionaryDetailList != null && dictionaryDetailList.size() > 0 ? 1 : 0;
			info = dictionaryDetailList != null && dictionaryDetailList.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			japaram = JSONArray.fromObject(dictionaryDetailList);
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}
	
}
